package com.fly.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.enums.CellExtraTypeEnum;
import com.fly.bean.excel.ExcelData;
import com.fly.listener.TemplateExcelListener;
import com.fly.mapper.UserMapper;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.List;

@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private UserMapper userMapper;

    @GetMapping("/hello")
    public String hello() {
        return "Hello World";
    }
    @Data
    public  class  A{
        private String name;
        private int age;
    }

    public static void lambda(List<A> list){
        list.sort(((o1,o2) -> {
            return o2.age - o1.age;
        }));
    }

    @PostMapping("/import")
    public void importExcel(HttpServletRequest request, MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            /**
             *DynamicEasyExcelListener:导入监听类
             * 1：是正文起始行，用于处理合并单元格
             **/
            TemplateExcelListener listener = new TemplateExcelListener(request, userMapper, 1);

            //读取全部sheet使用extraRead(CellExtraTypeEnum.MERGE).doReadAll()
            /**
             * headRowNumber(1)：1是正文起始行
             * CellExtraTypeEnum.MERGE：读取合并单元格
             * CellExtraTypeEnum.COMMENT：读取批注
             * CellExtraTypeEnum.HYPERLINK：读取超链接
             * extraRead(CellExtraTypeEnum.MERGE).doReadAll()：如果多sheet，直接使用这个方法
             **/
            EasyExcel.read(inputStream, ExcelData.class,listener)
                    .headRowNumber(1)
                    .extraRead(CellExtraTypeEnum.MERGE)
                    .sheet(0)
                    .doRead();
            inputStream.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
